// pages/car/car.js
import request from "../../utils/request"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        shopCar: [],
        allCheck: false,
        allMoney:0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getData()
        
    },
    async getData(params) {
        await request('/answer/getcar', params, {}, "GET").then((res) => {
            res.forEach((it) => {
                it.item = JSON.parse(it.item)
            })
            this.setData({
                shopCar: res
            })
        })
        let boo = this.data.shopCar.every(item => {
            return item.item.checked
        })
        this.setData({
            allCheck: boo
        })
        this.allMouey()
    },
    allMouey(){
        let allMoney = this.data.shopCar.filter(item => {
            return item.item.checked
        }).reduce((prev, next) => {
            return prev + parseInt(next.item.num * next.item.classPrice)
        }, 0)
        this.setData({
            allMoney
        })
    },
    addData(e) {
        let id = e.target.id
        let index = this.data.shopCar.findIndex(item => {
            return item.cid === id
        })
        this.data.shopCar[index].item.num += 1
        this.setData({
            shopCar:this.data.shopCar
        })
        this.allMouey()
    },
    cloneData(e) {
        let id = e.target.id
        let index =  this.data.shopCar.findIndex(cur => {
            return cur.cid === id
        })
        if ( this.data.shopCar[index].item.num > 1) {
             this.data.shopCar[index].item.num -= 1
             this.setData({
                 shopCar:this.data.shopCar
             })
        } else {
            let that = this
            wx.showModal({
                title: '已经是最后一个商品了',
                content: '确定要删除吗',
                success (res) {
                  if (res.confirm) {
                    that.data.shopCar.splice(index, 1)
                    that.setData({
                        shopCar:that.data.shopCar
                    })
                  } else if (res.cancel) {
                    that.data.shopCar[index].item.num = 1
                    that.setData({
                        shopCar:that.data.shopCar
                    })
                  }
                }
              })
        }
       this.allMouey()
    },
    checkboxChange(e) {
        let id = e.target.id
        console.log(e.target);
        let a = this.data.shopCar.find(item => {
            return item.cid === id
        })
        request('/answer/setCarList', {
            id,
            checked: Number(a.item.checked) === 0 ? 1 : 0
        }, {}, "POST")
        this.getData()
    },
    allCheck() {
        this.setData({
            allCheck: !this.data.allCheck ? 1 : 0
        })
        this.data.shopCar.forEach(item => {
            item.item.checked = this.data.allCheck
        })
        this.setData({
            shopCar:this.data.shopCar
        })
        this.allMouey()
    },
    Allaccount(e){
        this.setData({
            id:e.target.id
        })
        wx.navigateTo({
          url: `/pages/shopCarPatment/shopCarPatment?money=${this.data.allMoney}`,
        })
    },
    popup(e){
        console.log(e.target);
        wx.navigateTo({
          url: `/pages/particulars/particulars?cid=${e.target.id}`,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})